package com.hanhanxiaochong.zhongxiangchuanmei.widget;

import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.hanhanxiaochong.zhongxiangchuanmei.R;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.ExpressBean;
import com.hanhanxiaochong.zhongxiangchuanmei.config.HttpPostService;
import com.hanhanxiaochong.zhongxiangchuanmei.util.ResourcesUtils;
import com.hanhanxiaochong.zhongxiangchuanmei.view.order.adapter.LogisticsAdapter;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.Api.BaseObserver;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.Api.IBaseView;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.Api.ResponseBase;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.http.RetrofitSource;

import java.util.ArrayList;
import java.util.List;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by RockPhoenix on 2018/6/24.
 */

public class LogisticsDialog extends DialogFragment implements IBaseView {

    public static LogisticsDialog newInstance(Bundle bundle) {
        LogisticsDialog dialog = new LogisticsDialog();
        dialog.setArguments(bundle);
        return dialog;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        //去掉dialog的标题，需要在setContentView()之前
        this.getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        Window window = this.getDialog().getWindow();
        //去掉dialog默认的padding
        assert window != null;
        window.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = (int) (dm.widthPixels * 0.8f);
        lp.height = (int) (dm.heightPixels * 0.7f);
        //设置dialog的动画
//        lp.windowAnimations = R.style.BottomDialogAnimStyle;
        window.setAttributes(lp);
        window.setBackgroundDrawable(new ColorDrawable());

        String order_id = getArguments().getString("order_id");
        String address = getArguments().getString("address");
        View view = ResourcesUtils.inflate(R.layout.dialog_logistics);
        RecyclerView recyclerView = view.findViewById(R.id.rv_logistics);
        ImageView ivClose = view.findViewById(R.id.iv_close);
        ivClose.setOnClickListener(v -> {
            dismiss();
        });
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        List<ExpressBean.DataBean> list = new ArrayList<>();
        ExpressBean.DataBean dataBean = new ExpressBean.DataBean();
        dataBean.setContext(address);
        list.add(dataBean);

        RetrofitSource.createApi(HttpPostService.class)
                .kuai(order_id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<ResponseBase<ExpressBean>>(this) {
                    @Override
                    public void onSuccess(ResponseBase<ExpressBean> response) {
                        list.addAll(response.getData().getData());
                        LogisticsAdapter logisticsAdapter = new LogisticsAdapter(getActivity(), list);
                        recyclerView.setAdapter(logisticsAdapter);
                    }
                });

        return view;
    }

    @Override
    public void showTip(String msg) {

    }

    @Override
    public void loadSuccess(Object object) {

    }

    @Override
    public void loadFailure(String errorMsg) {

    }

    @Override
    public void showLoadingDialog() {

    }

    @Override
    public void closeLoadingDialog() {

    }
}
